package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ww.yaodao.mapper.SaleMapper;
import com.ww.yaodao.model.bo.SaleDO;
import com.ww.yaodao.model.dto.SaleProductDTO;
import com.ww.yaodao.model.vo.sale.SaleRespVO;
import com.ww.yaodao.model.vo.sale.SaleVO;
import com.ww.yaodao.service.ProductService;
import com.ww.yaodao.service.SaleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wanziyan
 * @Date 2023-04-14:19
 */
@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    private SaleMapper saleMapper;
    @Autowired
    private ProductService productService;
    LambdaQueryWrapper<SaleDO> wrapper = new LambdaQueryWrapper<>();

    @Override
    public SaleRespVO getAllSaleList(SaleVO saleVO) {
        List<SaleProductDTO> saleProductDTOS = saleMapper.saleAndProduct();
        List<SaleProductDTO> collect = null;
        if (saleVO != null) {
            collect = saleProductDTOS.stream().filter(saleProductDTO -> {
                boolean passPame = StringUtils.isEmpty(saleVO.getPname()) || saleVO.getPname().equals(saleProductDTO.getPname());
                boolean passModify = StringUtils.isEmpty(saleVO.getModifyName()) || saleVO.getModifyName().equals(saleProductDTO.getModifyName());
                boolean passModifyTime = StringUtils.isEmpty(saleVO.getModifyTime()) || saleVO.getModifyTime().equals(saleProductDTO.getModifyTime());
                return passPame && passModify && passModifyTime;
            }).collect(Collectors.toList());

        }
        return SaleRespVO.builder().allList(collect).build();
    }

    @Override
    public SaleRespVO updateSaleList(SaleVO saleVO) {
        wrapper.eq(SaleDO::getId, saleVO.getId());
        SaleDO saleDO = new SaleDO();
        BeanUtils.copyProperties(saleVO, saleDO);
        int update = saleMapper.update(saleDO, wrapper);
        SaleRespVO allSaleList = getAllSaleList(null);
        allSaleList.setRow(update);
        return allSaleList;
    }
}
